<?php

/**
 * shoppingcart actions.
 *
 * @package    sf_sandbox
 * @subpackage shoppingcart
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class shoppingcartActions extends sfActions
{
    public function executeIndex()
    {
        if ($this->getUser()->getAttribute(Globals::SESSION_CART_TOTAL_ITEM, 0) == 0) {
            $this->setFlash('errorMsg', "<h2 id='cartEmptyText'>Your Shopping Cart is empty.</h2>");
            return $this->redirect('product/index?p='.Globals::LINK_PRODUCTS_NEW);
        }

        $arr = $this->getUser()->getAttribute(Globals::SESSION_CART_ITEMS, array());
        $totalItem = $this->getUser()->getAttribute(Globals::SESSION_CART_TOTAL_ITEM, 0);

        $results = array();
        $keyArr = array_keys($arr);
        foreach ($keyArr as $productId):
            $c = new Criteria();
            $c->add(CmsProductPeer::PRODUCT_ID, $productId);
            $product = CmsProductPeer::doSelectOne($c);
            if ($product) {
                //var_dump($arr[$productId]);
                //var_dump($productId);
                //exit();
                $product->setQtyInCart($arr[$productId]);
                $results[] = $product;
            }
        endforeach;
        //var_dump($results);
        $this->cmsProducts = $results;
        $this->totalItem = $totalItem;


        // get delivery group
        $c = new Criteria();
        $c->add(DeliveryGroupPeer::STATUS_CODE, Globals::STATUS_ACTIVE);
        $this->deliveryGroups = DeliveryGroupPeer::doSelect($c);

        $c = new Criteria();
        $c->add(AppSettingPeer::PARAMETER, Globals::CUSTOMER_SERVICE_ADDRESS);
        $this->customerAddress = AppSettingPeer::doSelectOne($c);
    }

    public function executeRemoveProduct() {
        $productId = $this->getRequestParameter('productId');

        $arr = $this->getUser()->getAttribute(Globals::SESSION_CART_ITEMS, array());
        $totalItem = $this->getUser()->getAttribute(Globals::SESSION_CART_TOTAL_ITEM, 0);
        if (isset($arr[$productId])) {
            $totalItem -= $arr[$productId];
            unset($arr[$productId]);
        }

        $this->getUser()->setAttribute(Globals::SESSION_CART_ITEMS, $arr);
        $this->getUser()->setAttribute(Globals::SESSION_CART_TOTAL_ITEM, $totalItem);

        return $this->redirect('shoppingcart/index');
    }

    public function executeAdd2cart()
    {
        $productId = $this->getRequestParameter('productId');
        $cartQuantity = $this->getRequestParameter('cartQuantity');

        $arr = $this->getUser()->getAttribute(Globals::SESSION_CART_ITEMS, array());
        $totalItem = $this->getUser()->getAttribute(Globals::SESSION_CART_TOTAL_ITEM, 0);
        if (isset($arr[$productId])) {
            $qty = $arr[$productId];
            $arr[$productId] = $qty + $cartQuantity;
        } else {
            $arr = $this->array_push_assoc($arr, $productId, $cartQuantity);
        }
        $totalItem += $cartQuantity;

        $this->getUser()->setAttribute(Globals::SESSION_CART_ITEMS, $arr);
        $this->getUser()->setAttribute(Globals::SESSION_CART_TOTAL_ITEM, $totalItem);

        return $this->redirect('shoppingcart/index');
    }

    public function executeAddMany2cart()
    {
        $productId = $this->getRequestParameter('productId');
        $cartQuantity = $this->getRequestParameter('cartQuantity');
        //var_dump($productId);
        //var_dump("<br>");
        //var_dump($cartQuantity);
        $arr = $this->getUser()->getAttribute(Globals::SESSION_CART_ITEMS, array());
        $totalItem = $this->getUser()->getAttribute(Globals::SESSION_CART_TOTAL_ITEM, 0);

        if (isset($productId)) {
            for ($i = 0; $i < count($cartQuantity); $i++) {
                if ($cartQuantity[$i] > 0) {
                    if (isset($arr[$productId[$i]])) {
                        $qty = $arr[$productId[$i]];
                        $arr[$productId[$i]] = $qty + $cartQuantity[$i];
                    } else {
                        $arr = $this->array_push_assoc($arr, $productId[$i], $cartQuantity[$i]);
                    }
                    $totalItem += $cartQuantity[$i];
                }
            }
        }

        $this->getUser()->setAttribute(Globals::SESSION_CART_ITEMS, $arr);
        $this->getUser()->setAttribute(Globals::SESSION_CART_TOTAL_ITEM, $totalItem);

        return $this->redirect('shoppingcart/index');
    }

    function array_push_assoc($array, $key, $value)
    {
        $array[$key] = $value;
        return $array;
    }

    public function executeEmpty()
    {
    }
}
